<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * pengalaman.php
 * @author ranuandra
 *
 * 7 Mei 2012 13:32:40
 *
 * 
 */
class Controller_Pengalaman extends Kohana_Nasmoco
{
	public function action_index()
	{
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$datas = $pelamar->pelamarpengalaman->find_all();
		$view = View::factory('pengalaman/index');
		$view->datas = $datas;
		$this->template->container = $view;
	}
	
	public function action_add()
	{
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$bidang = ORM::factory('mstbidang')->find_all()->as_array('id', 'bidang');
		$spesialisasi = ORM::factory('mstspesialisasi')->find_all()->as_array('id', 'spesialisasi');
		$posisi = ORM::factory('mstposisi')->find_all()->as_array('id', 'posisi');
		$industri = ORM::factory('mstindustri')->find_all()->as_array('id', 'industri_type');
		$view = View::factory('pengalaman/form');
		$view->pelamar = $pelamar;
		$view->bidanglist = $bidang;
		$view->spesialisasilist = $spesialisasi;
		$view->posisilist = $posisi;
		$view->industrilist = $industri;
		$this->template->container = $view;
	}
	
	public function action_save()
	{
			$db = Database::instance();
			$db->begin();
			
			try 
			{
				if(isset($_POST['id']))
				{
					$pengalaman = ORM::factory('pelamarpengalaman', Request::current()->post('id'));
					$pengalaman->mstbidang_id = Request::current()->post('mstbidang_id');
					$pengalaman->lama = Request::current()->post('lama');
					$pengalaman->institusi = Request::current()->post('institusi');
					$pengalaman->alamat_institusi = Request::current()->post('alamat_institusi');
					$pengalaman->cp = Request::current()->post('cp');
					$pengalaman->save();
				}
				else 
				{
					$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
					if(isset($_POST['levelpengalaman']))
					{
						
						$pelamar->levelpengalaman = $_POST['levelpengalaman'];
						$pelamar->save();
					}
					if(isset($_POST['institusi']))
					{
						$pengalaman = ORM::factory('pelamarpengalaman');
						$pengalaman->mstpelamar_id = $pelamar->id;
						$pengalaman->dari_tanggal = Request::current()->post('dr_tahun').'-'.Request::current()->post('dr_bulan').'-1';
						$pengalaman->sampai_tanggal = Request::current()->post('sp_tahun').'-'.Request::current()->post('sp_bulan').'-1';
						$pengalaman->create_data($_POST);
					}
					
				}
				
				$db->commit();
				Request::current()->redirect(URL::base().'user');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}	
	}
}